package com.salt.admin.api.service;

import com.salt.admin.api.model.DataRule;
import com.salt.admin.api.persistence.BaseEntity;
import com.salt.admin.api.utils.UserClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service基类
 * @author salt
 * @version 2017-05-16
 */
@Transactional(readOnly = true)
public abstract class BaseService {

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	
	/**
	 * 数据范围过滤
	 * @param entity 当前过滤的实体类
	 */
	public static void dataRuleFilter(BaseEntity<?> entity) {
		if(UserClientUtils.getUser() == null ||  StringUtils.isBlank(UserClientUtils.getUser().getId())){
			return;
		}
		entity.setCurrentUser(UserClientUtils.getUser());
		List<DataRule> dataRuleList = UserClientUtils.getDataRuleList();

		// 如果是超级管理员，则不过滤数据
		if (dataRuleList.size() == 0) {
			return;
		}

		// 数据范围
		StringBuilder sqlString = new StringBuilder();


			for(DataRule dataRule : dataRuleList){
				if(entity.getClass().getSimpleName().equals(dataRule.getClassName())){
					sqlString.append(dataRule.getDataScopeSql());
				}

			}

		entity.setDataScope(sqlString.toString());

	}

}
